package homework.dao;

import java.io.EOFException;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;


import homework.db.Filepath;
import homework.entity.User;






//编写数据库语言操作，这里替换成对文件的操作
public class UserDaoImpl implements IUserDao{
		
	static List<User> ulist = new ArrayList<User>();
	String path="E:\\eclipse-workspace\\homework\\src\\user.txt";	
	private static int flag=0;
	
	public void init() {
		
	}
	
	public void filein() {
		FileInputStream fin;
		ObjectInputStream obin;
		if(flag==0) {
			return;
		}else {
			try {
				File f= new File(path);
				fin = new FileInputStream(f);
				obin= new ObjectInputStream(fin);
				
				ulist = (ArrayList<User>)obin.readObject();
//					for(int i=0;i<flag;i++) {		
//						getUlist().add((User)obin.readObject());
//					}
				obin.close();
				fin.close();
			
			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
			
	}
	
	
	public void addUser(User user) {
		
		FileOutputStream fout;
		ObjectOutputStream obout;
		try {
			//避免重复添加，节约内存
			if(getUlist()!=null) {
				for(User u:getUlist()) {
					if(u.equals(user)) {
						System.out.println("用户已添加");
						return;
					}
				}
			}
			
			File file =  new File(path);
			//fout = new FileOutputStream(file,true);
			
			filein();
			fout = new FileOutputStream(file);
			obout = new ObjectOutputStream(fout);
			getUlist().add(user);
			obout.writeObject(getUlist());			
			obout.close();
			fout.close();
			flag++;
			filein();
			
 		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}
	
		public void changeUser(User user) {
			filein();
			int count=0;
			for(User u:getUlist()) {
				if(u.getUsername().equals(user.getUsername())) {
					getUlist().set(count, user);
				}
				count++;
			}
			
			try {
				File file=new File(path);
				ObjectOutputStream obout = new ObjectOutputStream(new FileOutputStream(file));
				obout.writeObject(getUlist());
			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			
			
		}
		
		public void removeUser(User user) {
			filein();
			
			//for(User u:getUlist()) {
			//引发多线程并发修改非线程安全集合异常
			for(int i=0;i<flag;i++) {
				if(getUlist().get(i).getUsername().equals(user.getUsername())) {
					getUlist().remove(i);
					flag--;
				}
				
			}
			try {
				File file=new File(path);
				ObjectOutputStream obout = new ObjectOutputStream(new FileOutputStream(file));
				obout.writeObject(getUlist());
			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
		//打印所有数据到控制台，主要用于对文件操作的代码测试，检验代码的运行过程。同时更新ulist列表数据
	public void printAll() {
		filein();
		for(int i=0;i<flag;i++) {
			
			System.out.println(getUlist().get(i));
		}
	}
	//这里一方面输出查询到的符合的user对象，同时返回这个对象。在后面的service服务中使用的非常多。
	public User printUser(String username) {
		filein();
		User u1=null;
		for(User u:getUlist()) {
			if(u.getUsername().equals(username)) {
				System.out.println(u);
				u1=u;
			}
			
		}
		return u1;
	}


	public List<User> getUlist() {
		System.out.println(ulist);
		return ulist;
	}


	

}




	



